/**
 * 
 */
package com.wgo.precise.client.ui.view.util.celleditors;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.SelectionDialog;

import com.wgo.precise.client.ui.controller.ModelStatus;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.view.util.URLHyperLinkLabel;
import com.wgo.precise.client.ui.view.util.URLHyperLinkListener;
import com.wgo.precise.client.ui.view.util.dialogs.LinkedTextInputDialog;


/**
 * @author petterei
 *
 * @version $Id: LinkedTextCellEditor.java,v 1.2 2006-03-07 19:45:51 petterei Exp $
 */

public class LinkedTextCellEditor extends DialogCellEditor {

	private URLHyperLinkListener urlListener;
	private URLHyperLinkLabel linkLabel;
	public LinkedTextCellEditor(Composite parent, int style) {
		super(parent, style);
		urlListener  = new URLHyperLinkListener();
	}

	public LinkedTextCellEditor(Composite parent) {
		super(parent);
		urlListener  = new URLHyperLinkListener();
	}

	/**
	 * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(org.eclipse.swt.widgets.Control)
	 */
	@Override
	protected Object openDialogBox(Control cellEditorWindow) {
		Object value = getValue();
		String stringValue = null;
		if (null != value) {
			stringValue = ((URL)value).toExternalForm();
		}
		LinkedTextInputDialog dialog = new LinkedTextInputDialog(cellEditorWindow.getShell(),"Text area", "Enter the text", stringValue) ;
		if (dialog.open() == SelectionDialog.OK && dialog.getValue().length() > 0){
			Object dialogValue = dialog.getValue();
			if (null != dialogValue) {
				try {
					value = new URL((String)dialogValue);
				} catch (MalformedURLException e) {
					RequirementPlugin.log(new ModelStatus(IStatus.ERROR, "URL input error", e));
				}
			}
		}
		return value;
	}

	/**
	 * @see org.eclipse.jface.viewers.DialogCellEditor#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite cell) {
		linkLabel = new URLHyperLinkLabel(cell, SWT.LEFT);
		linkLabel.setFont(cell.getFont());
		linkLabel.setBackground(cell.getBackground());
		linkLabel.setVisible(true);
		return linkLabel;
	}

	/**
	 * @see org.eclipse.jface.viewers.DialogCellEditor#getDefaultLabel()
	 */
	@Override
	protected Label getDefaultLabel() {
		return linkLabel;
	}


	/**
	 * @see org.eclipse.jface.viewers.DialogCellEditor#updateContents(java.lang.Object)
	 */
	@Override
	protected void updateContents(Object value) {
		if (null == linkLabel) {
			RequirementPlugin.log(new ModelStatus(IStatus.WARNING, getClass().getSimpleName() + "linked label was null.. [memref: "+hashCode()+"]"));
			return;
		} 
		RequirementPlugin.log(new ModelStatus(IStatus.INFO, getClass().getSimpleName() + " linked label is visible.. [memref: "+hashCode()+"]"));

        String text = "";//$NON-NLS-1$
        if (value != null) {
            text = value.toString();
            linkLabel.setHref((URL)value);
    		linkLabel.addHyperlinkListener(urlListener);
        } else {
    		linkLabel.removeHyperlinkListener(urlListener);        	
        }
        linkLabel.setText(text);
        linkLabel.setVisible(true);
	}

	/**
	 * @see org.eclipse.jface.viewers.CellEditor#activate()
	 */
	@Override
	public void activate() {
		super.activate();
		if (null == linkLabel) {
			RequirementPlugin.log(new ModelStatus(IStatus.WARNING, getClass().getSimpleName() + " linked label was null (activation)..  [memref: "+hashCode()+"]"));
		} else {
			linkLabel.setVisible(true);
		}
	}
	
}
